package com.leo.interceptor;

import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;

/**
 * @ClassName: HandlerInterceptor5
 * @Description: 实现WebRequestInterceptor来实现拦截
 * @Author: leo825
 * @Date: 2020-02-08 20:42
 * @Version: 1.0
 */
public class HandlerInterceptor5 implements WebRequestInterceptor {

    @Override
    public void preHandle(WebRequest webRequest) throws Exception {
        System.out.println("HandlerInterceptor5 preHandle");
        webRequest.setAttribute("startTime", System.currentTimeMillis(), RequestAttributes.SCOPE_SESSION);
    }

    @Override
    public void postHandle(WebRequest webRequest, ModelMap modelMap) throws Exception {
        System.out.println("HandlerInterceptor5 postHandle");
    }

    @Override
    public void afterCompletion(WebRequest webRequest, Exception e) throws Exception {
        System.out.println("HandlerInterceptor5 afterCompletion");
        long startTime = (long) webRequest.getAttribute("startTime", RequestAttributes.SCOPE_SESSION);
        System.out.println("HandlerInterceptor5 过滤的接口耗时：" + (System.currentTimeMillis() - startTime) + "ms");
    }
}
